<?php

namespace App\Admin\Controllers;

use App\Models\Base\Item;
use App\Models\Game\ShopItemTable;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

/**
 * 交易中心.
 */
class TradingController extends Controller
{
    /**首页
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.order_stat'));
            $content->description(trans('game.deal_stat'));
            $content->body($this->grid());
        });
    }

    /**视图
     * @return Grid
     */
    protected function grid()
    {
        $grid = Admin::grid(ShopItemTable::class, function (Grid $grid) {
            //模糊搜索道具名
            $name = Input::get('name');
            if ($name) {
                $item_ids = Item::where('name', 'like', "%{$name}%")->pluck('item_no')->toArray();
                if (!empty($item_ids)) {
                    $grid->model()->whereIn('itemdataid', $item_ids);
                }
            }
            $grid->id();
            $grid->itemdataid(trans('game.item_id'));
            $grid->column(trans('game.item_name'))->display(function () {
                $item = Item::find($this->itemdataid);

                return $item ? $item->name : trans('game.no_name');
            });
            $grid->type('类型')->display(function ($type) {
                return 1 == $type ? trans('game.easy') : trans('game.futures');
            });
            $grid->storeMax(trans('game.tot_inventory'))->sortable();
            $grid->storeNum(trans('game.current_inventory'))->sortable();
            $grid->saleInNum(trans('game.buy_number'))->sortable();
            $grid->saleOutNum(trans('game.sale_number'))->sortable();
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('type', trans('game.type'))->select([1 => trans('game.easy'), 2 => trans('game.futures')]);
                $filter->equal('name', trans('game.item_name'));
                $filter->setNotWhere(['name']);
            });
        });

        return $grid;
    }
}
